package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.Room;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IRoomService;
import com.zzyl.nursing.vo.BedVo;
import com.zzyl.nursing.vo.DeviceVo;
import com.zzyl.nursing.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 房间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }


    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    @Override
    public RoomVo one(Integer id) {
        return roomMapper.one(id);
    }

    @Override
    public List<RoomVo> getRoomsWithDeviceByFloorId(Long floorId) {
        //传入楼层id，得到该楼层的房间列表
        List<RoomVo> roomList = roomMapper.getRoomsWithDeviceByFloorId(floorId);
        //判断列表是否为空
        if(CollUtil.isNotEmpty(roomList)){
            //不为空，遍历房间列表
            for (RoomVo roomVo : roomList) {
                //得到房间的设备列表
                List<DeviceVo> deviceVos = roomVo.getDeviceVos();
                //判断设备列表是否为空
                if(CollUtil.isNotEmpty(deviceVos)){
                    //不为空，遍历设备列表
                    for (DeviceVo deviceVo : deviceVos) {
                        //根据设备id从缓存拿数据
                        String deviceDataListStr = (String)redisTemplate.boundHashOps(CacheConstants.IOT_DEVICE_LAST_DATA).get(deviceVo.getIotId());
                        //判断拿到的数据是否为空
                        if(StrUtil.isNotEmpty(deviceDataListStr)){
                            //不为空，数据转为列表
                            List<DeviceData> deviceData = JSONUtil.toList(deviceDataListStr, DeviceData.class);
                            //将数据列表放到返回的数据vo
                            deviceVo.setDeviceDataVos(deviceData);
                        }
                    }
                }
                //得到房间的床位列表
                List<BedVo> bedVoList = roomVo.getBedVoList();
                //判断
                if(CollUtil.isNotEmpty(bedVoList)){
                    //遍历
                    for (BedVo bedVo : bedVoList) {
                        //床位的设备列表
                        List<DeviceVo> deviceVos1 = bedVo.getDeviceVos();
                        //判断
                        if(CollUtil.isNotEmpty(deviceVos1)){
                            //遍历
                            for (DeviceVo deviceVo : deviceVos1) {
                                //从缓存拿数据
                                String deviceDataListStr = (String)redisTemplate.boundHashOps(CacheConstants.IOT_DEVICE_LAST_DATA).get(deviceVo.getIotId());
                                //判断
                                if(StrUtil.isNotEmpty(deviceDataListStr)){
                                    //转List
                                    List<DeviceData> deviceData = JSONUtil.toList(deviceDataListStr, DeviceData.class);
                                    //写入vo
                                    deviceVo.setDeviceDataVos(deviceData);
                                }
                            }
                        }
                    }
                }
            }
        }
        return roomList;
    }
}
